
import java.io.*;
import java.net.*;
import java.util.StringTokenizer;

public class SimulatedLogin {
	
	private String session_id;
	
	public static void main(String[] args) throws Exception{
		
		SimulatedLogin sl = new SimulatedLogin();
		sl.downloadVerifiedImage();
		sl.login("00948135","19900516");
		sl.logout();
	}
	
	/*
	private void DownloadPages() throws Exception{
		try{
			URL url = new URL("http://dean.pku.edu.cn/student/");
			HttpURLConnection connection =  (HttpURLConnection) url.openConnection();
			connection.setRequestProperty("User-Agent", "Mozilla/5.0");
			connection.connect();
			InputStream in = connection.getInputStream();
			BufferedReader breader = new BufferedReader(new InputStreamReader(in,"GBK"));
			String str = breader.readLine();
			while(str != null){
				System.out.println(str);
				str = breader.readLine();
			}
		}catch (Exception e){
			e.printStackTrace();
		}
	}
	*/

	private String getSessionID(HttpURLConnection conn) throws Exception{
		StringBuffer cookies = new StringBuffer();
		String sid = null;
		String head_name = null;
		head_name = conn.getHeaderField(4);//select Set-Cookie: PHPSESSID=183bbfd1fd546e024c2c499cce3fd9b6; path=/
		StringTokenizer st = new StringTokenizer(head_name,":");
		while(st.hasMoreTokens()){
			cookies.append(st.nextToken());
		}
		StringTokenizer st2 = new StringTokenizer(cookies.toString(),"=;");
		int i = 0;
		while(st2.hasMoreElements()){
			sid = st2.nextToken();
			i ++;
			if ( i == 2)
				break;
		}
		return sid;
	}
	
	private void downloadVerifiedImage() throws Exception{
		try{
			System.out.println("Begin to Download Verified Image");
			URL url = new URL("http://dean.pku.edu.cn/student/yanzheng.php?act=init");
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestProperty("User-Agent", "Mozilla/5.0");
			conn.setRequestMethod("GET");
			conn.connect();
			InputStream in = conn.getInputStream();
			//store verified img
			String img_name = "verified_img.png";
			File img = new File(img_name);
			BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(img));
			byte[] buf = new byte[2048];
			int length = in.read(buf);
			while(length != -1){
				bout.write(buf, 0, length);
				length = in.read(buf);
			}
			in.close();
			bout.flush();
			bout.close();
			System.out.println("Download Verified Image Successful!");
			
			//get session_id
			session_id = getSessionID(conn);
			System.out.println("session_id: " + session_id);
		}catch (Exception e){
			e.printStackTrace();
		}
	}
	
	
	private void login(String sno, String passwd) throws Exception{
		try{
			URL url = new URL("http://dean.pku.edu.cn/student/center.php");
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			//allow output
			conn.setDoOutput(true);
			//set post method
			conn.setRequestMethod("POST");
			//set post content
			conn.setRequestProperty("User-Agent", "Mozilla/5.0");
			conn.setRequestProperty("Cookie", "PHPSESSID=" +session_id );
			//set request string
			StringBuffer sb = new StringBuffer();
			sb.append("database=0");
			sb.append("&sno="+sno);
			sb.append("&passwd="+passwd);
			//input verified code
			System.out.println("please input the verified code:");
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			String verified_code = br.readLine();
			System.out.println("verified_code:" +verified_code);
			sb.append("&number="+verified_code);
			System.out.println(sb.toString());
			//write post info to out
			OutputStream os = conn.getOutputStream();
			os.write(sb.toString().getBytes("GBK"));
			os.close();
			//connect
			conn.connect();
			//read
			BufferedReader br2 = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String line = br2.readLine();
			while(line != null){
				System.out.println(line);
				line = br2.readLine();
			}
			System.out.println("Login Successful!");

			
		}catch (Exception e){
			e.printStackTrace();
		}
	}
	
	private void logout() throws Exception{
		try{
			URL url = new URL("http://dean.pku.edu.cn/student/exit.php?PHPSESSID=" + session_id);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			//set post method
			conn.setRequestMethod("GET");
			//set post content
			conn.setRequestProperty("User-Agent", "Mozilla/5.0");
			conn.setRequestProperty("Cookie", "PHPSESSID=" +session_id );
			//connect
			conn.connect();
			//read
			BufferedReader br2 = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String line = br2.readLine();
			while(line != null){
				System.out.println(line);
				line = br2.readLine();
			}
			System.out.println("Logout Successful!");
		}catch (Exception e){
			e.printStackTrace();
		}
	}
	
	
	
	
}
